﻿// project      : soapi-notify 
// purpose      : console and growl notifications of new Stack Exchange questions
// source       : http://soapi.info/tools/notify.aspx
// announcement : http://stackapps.com/questions/820
// website      : http://soapi.info/tools/notify.aspx
// license      : MIT/X11 - Copyright (c) 2010 Sky Sanders - See License.txt
// inspiration  : http://stackapps.com/questions/817/senotify-se-new-question-notification
// 
//  
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Soapi.Notify
{
    public partial class GuiForm : Form
    {
        public GuiForm()
        {
            InitializeComponent();
            SitesCheckedListBox.SetItemChecked(0, true);
            BuildCommandLine();
            UpdateGrowlUI();
        }

        private void UpdateGrowlUI()
        {
            Installer.RegisterApp();
            if (!Installer.GrowlInstalled || !Installer.DisplaysInstalled || !Installer.DisplaysCurrent)
            {
                GrowlCheckBox.Enabled = false;
                UpdateGrowlCheckBox.Enabled = false;
                InstallUpgradeDisplaysButton.Enabled = Installer.GrowlInstalled;
            }
            else
            {
                GrowlCheckBox.Enabled = true;
                UpdateGrowlCheckBox.Enabled = true;
                InstallUpgradeDisplaysButton.Enabled = false;
            }
        }
        public int PollingInterval
        {
            get { return (int)PollingIntervalNumericUpDown.Value; }
        }

        private void BuildCommandLine(object sender, EventArgs e)
        {
            BuildCommandLine();
        }

        private void BuildCommandLine()
        {
            var sb = new StringBuilder();


            if (PollingIntervalNumericUpDown.Value > 60)
            {
                sb.AppendFormat(" -p {0}", (int)PollingIntervalNumericUpDown.Value);
            }

            if (GrowlCheckBox.Checked)
            {
                sb.Append(" -g");
            }
            if (UpdateGrowlCheckBox.Checked)
            {
                sb.Append(" -u");
            }
            if (TagsExcludeRadioButton.Checked)
            {
                sb.Append(" -x");
            }


            if (TagsTextBox.Text.Trim().Length > 0)
            {
                sb.AppendFormat(" -t {0}", Regex.Replace(TagsTextBox.Text, @"\s+", ";").Trim());
            }

            sb.AppendFormat(" -s {0}", string.Join(";", SitesCheckedListBox.CheckedItems.Cast<string>().ToArray()));

            CommandLineTextBox.Text = sb.ToString().Trim();
        }


        private void CancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void RunCommandLineButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void GuiForm_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            Process.Start("http://soapi.info/tools/notify.aspx");
        }

        private void GrowlLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.growlforwindows.com/gfw/");
        }

        private void SetupGrowlLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://soapi.info/tools/notify.aspx#display");
        }

        private void InstallUpgradeDisplaysButton_Click(object sender, EventArgs e)
        {
            while (Installer.GrowlIsRunning)
            {
                DialogResult result =
                    MessageBox.Show(
                        "Displays cannot be installed/upgraded while Growl is running.\r\nPlease stop Growl and click OK");
                if (result != DialogResult.OK)
                {
                    return;
                }
            }
            try
            {
                Installer.InstallDisplays();
                Installer.RegisterApp();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            UpdateGrowlUI();

        }

        private void GuiForm_Activated(object sender, EventArgs e)
        {
            UpdateGrowlUI();
        }

        private void GuiForm_Shown(object sender, EventArgs e)
        {
            this.TopMost = true;
            Application.DoEvents();
            TopMost = false;
        }
    }
}